/*
* This is the main game screen
* It is the top of the scene graph for when the game is being played
*/

#include "GameScreen.h"
#include "Camera.h"
#include "MuncherShape.h"
#include "BoardCell.h"
#include "GameRules.h"
#include "StringManager.h"


GameScreen::GameScreen()
{
	board = GameRules::Instance()->GenerateBoard();
	player1 = new Muncher();
	
	Shape * s = new MuncherShape();

	scene->AddChild((SceneNode*)new Camera());
	scene->AddChild((SceneNode*)board); 

	scene->AddChild((SceneNode*)player1);
}

GameScreen::~GameScreen()
{
	scene->Release();
}

void GameScreen::Draw()
{
	Screen::Draw(); // Call the super class
	
	FontRender2D * fr = FontRender2D::Instance();
	StringManager sm;
	GameRules * gr = GameRules::Instance();

	//GameRules::Instance()->GetLevel();
	fr->DrawText(0,0,L"Level: " + sm.ToStr(GameRules::Instance()->GetLevel()));
	fr->DrawText(400,0,gr->GetLevelString());

	//Debug
	//fr->DrawText(0,30, L"Level: " + sm.ToStr(gr->GetLevel()));
	//fr->DrawText(0,60, L"LevelNumber: " + sm.ToStr(gr->GetLevelNumber()));


}

void GameScreen::Update(double frameTime)
{
	Screen::Update(frameTime);


	GameRules * gameRules = GameRules::Instance();
	Controls * controls = Controls::Instance();

	if( gameRules->BoardComplete(board) )
	{
		srand(GetTickCount());
		gameRules->SetLevel(gameRules->GetLevel() + 1);
		//gameRules->SetLevelNumber(25);
		*board = *gameRules->GenerateBoard();
	}

	BYTE pressedKey;

	if ( player1->velocity->x || player1->velocity->z )	
		pressedKey = 0;	
	else
		pressedKey = controls->GetPressedKey();

	//TODO: make keyboard handeling better
	//TODO: i wonder if this should be in the muncher update method
	if( pressedKey == DIK_LEFT)
	{
		if (player1->boardPosX > 0)
			player1->MoveLeft();
	}
	else if ( pressedKey == DIK_RIGHT)
	{
		if (player1->boardPosX < 4 ) //TODO: get the size from the board
			player1->MoveRight();
	}
	else if ( pressedKey == DIK_UP)
	{
		if( player1->boardPosY > 0 )
			player1->MoveUp();
	}
	else if ( pressedKey == DIK_DOWN)
	{
		if( player1->boardPosY < 3 )
			player1->MoveDown();
	}
	else if ( pressedKey == DIK_SPACE)
	{
		BoardCell * boardCell = board->boardCells[player1->boardPosX][player1->boardPosY];
		//Eat number
		
		if ( gameRules->IsEdible(boardCell) ) 
		{
			boardCell->SetEaten(true);
		}

	}
	
}

